// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "TSG.h"

#include "MainFrm.h"
#include "TSGDoc.h"
#include "TSGView.h"
#include "TSTreeView.h"
#include "LogView.h"
#include "DVBDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_TSTYPE, OnUpdateTSType)
	ON_COMMAND(ID_TS_ANALYZE, &CMainFrame::OnTsAnalyze)
	ON_WM_TIMER()
	ON_WM_SIZE()
	ON_MESSAGE(WM_UPDATETREE, OnUpdateTree)
	ON_COMMAND(ID_TOOLS_DVBRULES, &CMainFrame::OnToolsDvbrules)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_PROGRESS,
	ID_INDICATOR_TSTYPE,
};


// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	m_bSplitterWndCreated = FALSE;
}

CMainFrame::~CMainFrame()
{
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	m_wndStatusBar.SetPaneInfo(1, ID_INDICATOR_PROGRESS, SBPS_NORMAL, 200);

	// TODO: Delete these three lines if you don't want the toolbar to be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}


// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG


// CMainFrame message handlers
void CMainFrame::OnUpdateTSType(CCmdUI* pCmdUI)
{
	CTSGDoc* pDoc = (CTSGDoc*)GetActiveDocument();
	CTSGConfig *pConfig = pDoc->GetConfig();

	pCmdUI->Enable();
	CString str;
	if(pConfig->GetType() == 0)
	{
		str.Format(_T("TS:unknown"));
	}
	else
	{
		str.Format(_T("TS:%d"),pConfig->GetType());
	}
	pCmdUI->SetText(str);
}

static UINT AFX_CDECL StartAnalyze(LPVOID pParam)
{
	CTSGDoc* pDoc = (CTSGDoc*)pParam;

	pDoc->AnalyzeStream();

	return 0;
}


void CMainFrame::OnTsAnalyze()
{
	// TODO: Add your command handler code here
	CTSGDoc* pDoc = (CTSGDoc*)GetActiveDocument();
	SetTimer(1, 1000, NULL);
	SetTimer(2, 200, NULL);

	AfxBeginThread(StartAnalyze, pDoc, 0, 0, 0, 0);
	
}

void CMainFrame::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	CTSGDoc* pDoc = (CTSGDoc*)GetActiveDocument();
	if(nIDEvent == 1)
	{
		if(pDoc->GetProgress()<100)
		{
			m_wndStatusBar.ShowProgressBar();
			m_wndStatusBar.SetProgress(pDoc->GetProgress());
		}
		else
		{
			m_wndStatusBar.SetProgress(100);
			KillTimer(1);
			m_wndStatusBar.HideProgressBar();
			m_wndStatusBar.SetPaneText(1, _T("Done!"), TRUE);
		}
	}

	if(nIDEvent == 2)
	{
		LPTSTR lpszLog = NULL;
		BOOL bUpdated = FALSE;

		lpszLog = theLogFile.GetLogBuffer(bUpdated);
		if(bUpdated)
		{
			m_SplitterWnd.GetPane(1,0)->SetWindowText(lpszLog);
			INT nLine = ((CLogView*)m_SplitterWnd.GetPane(1,0))->GetEditCtrl().GetLineCount();
			nLine = (nLine < MAX_NODE)?nLine:MAX_NODE;
			((CLogView*)m_SplitterWnd.GetPane(1,0))->GetEditCtrl().LineScroll(nLine,0);
		}
		
	}

	//CFrameWnd::OnTimer(nIDEvent);
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
	// TODO: Add your specialized code here and/or call the base class
	//if (m_SplitterWnd.CreateStatic(this, 2, 1, WS_CHILD | WS_VISIBLE, ID_SPLIT_WINDOW) == FALSE)
	if (m_SplitterWnd.CreateStatic(this, 2, 1) == FALSE)
	{
		return FALSE;
	}

	CRect rc;
	GetWindowRect(&rc);

	//Create upper view
	if (m_SplitterWnd.CreateView(0, 0, RUNTIME_CLASS(CTSTreeView), CSize(rc.Width(), rc.Height()*2/3), pContext)== FALSE)
	{
		return FALSE;
	}

	//Create buttom view
	if (m_SplitterWnd.CreateView(1, 0, RUNTIME_CLASS(CLogView), CSize(rc.Width(), rc.Height()/3), pContext)==FALSE)
	{
		return FALSE;
	}
	m_SplitterWnd.RecalcLayout();

	//SetActiveView((CView*)m_SplitterWnd.GetPane(0, 0));

	m_bSplitterWndCreated = TRUE;

	return TRUE;
}

void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
	CFrameWnd::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
	CRect rc;
	GetClientRect(&rc);

	if(!m_bSplitterWndCreated)
	{
		return;
	}

	if(rc.Height() >= 450 || rc.Height() < 150)
	{
		m_SplitterWnd.SetRowInfo(0, rc.Height()*2/3, rc.Height()*2/3);
	}
	else
	{
		m_SplitterWnd.SetRowInfo(0, rc.Height()-150, rc.Height()-150);
	}
	m_SplitterWnd.RecalcLayout();
}

afx_msg LRESULT CMainFrame::OnUpdateTree(WPARAM wParam, LPARAM lParam)
{
	CTreeNode* pNode = (CTreeNode*)wParam;

	((CTSTreeView*)m_SplitterWnd.GetPane(0, 0))->UpdateTree(pNode);
	return 0;
}


void CMainFrame::OnToolsDvbrules()
{
	// TODO: Add your command handler code here
	CDVBDialog dialog;

	if(dialog.DoModal()==IDOK)
	{
	}
}
